package com.hh.releasenode.shiro;

import com.hh.releasenode.bean.RedisBean;
import com.hh.releasenode.dao.SysUserDao;
import com.hh.releasenode.entity.SysUserEntity;
import com.hh.releasenode.service.SysRoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class ShiroRealm extends AuthorizingRealm {

	@Resource
	private SysUserDao sysUserDao;

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private RedisBean redisBean;

	/**
	 * 获取用户角色和权限
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
		SysUserEntity user = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		Long id = user.getId();
		System.out.println(id);
		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		//获取用户角色集合

//		sysRoleService.


//		simpleAuthorizationInfo.setRoles();


		//获取用户权限菜单


		return null;
	}

	/**
	 * 登录认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String accessToken = (String) token.getCredentials();//credentials 提供证明的

//		System.out.println("用户token" + accessToken + "认证-----ShiroRealm.doGetAuthenticationInfo");

		SysUserEntity user = (SysUserEntity)redisBean.get(accessToken);

		if (user == null) {
			throw new UnknownAccountException("token 失效！");
		}
//
		if (user.getStatus().equals("0")) {
			throw new LockedAccountException("账号已被锁定,请联系管理员！");
		}
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, token, getName());

		return info;
	}

}
